/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
 */
package VMTermClassSettingsDialog;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import GuiTools.*;
import Mainframe.Mainframe;
import Settings.*;
import Storage.*;
import VocabularyManagement.EditVocDialog;
import javax.swing.*;
import javax.swing.event.*;

public class ApplyTCChangesDialog {
	private MDTribuitFrame frame;
	private TCsPanel tcsPanelA, tcsPanelB;
	private Vector<String> editedTCsA, editedTCsB, newSortedKeysA,newSortedKeysB;
	private HashMap<String, TermClass> newTCsA, newTCsB;
	private HashMap<String, ATCCDTermClassPanel> convertedTCsA = new HashMap<String, ATCCDTermClassPanel>(),convertedTCsB = new HashMap<String, ATCCDTermClassPanel>();
	private Vocabulary vocToEdit;
	private Settings SET;
	private Language LANG;
	private Storage MAINVOC;
	private MDManager VMDM;
	private Mainframe MAINFRAME;
	private boolean changesMadeA, changesMadeB;
	final private MDTribuitFrame vsdFrame;

	/**
	 * In this dialog the effects of changed term class settings on the
	 * vocabulary data are set.
	 * 
	 * @author Christoph Schnack
	 * 
	 */
	public ApplyTCChangesDialog(Vocabulary vte, Storage mv, Settings set,
			Mainframe mf, MDManager mdm, MDTribuitFrame vFrame,
			Vector<String> eTCsA, HashMap<String, TermClass> nTCsA,
			Vector<String> NSKA, Vector<String> eTCsB,
			HashMap<String, TermClass> nTCsB, Vector<String> NSKB) {
		vsdFrame = vFrame;
		editedTCsA = eTCsA;
		editedTCsB = eTCsB;
		newSortedKeysA = NSKA;
		newSortedKeysB = NSKB;
		newTCsA = nTCsA;
		newTCsB = nTCsB;
		vocToEdit = vte;
		SET = set;
		LANG = SET.getLanguage();
		MAINVOC = mv;
		MAINFRAME = mf;
		VMDM = mdm;
		changesMadeA = eTCsA.size() > 0;
		changesMadeB = eTCsB.size() > 0;
		buildGUI();
	}

	private void buildGUI() {
		frame = new MDTribuitFrame(LANG.getEffectsOnWordData(), VMDM);

		tcsPanelA = new TCsPanel(editedTCsA, vocToEdit.getTermclassesA(),
				newTCsA, newSortedKeysA, convertedTCsA);
		tcsPanelB = new TCsPanel(editedTCsB, vocToEdit.getTermclassesB(),
				newTCsB, newSortedKeysB, convertedTCsB);
		JTabbedPane jtb = new JTabbedPane();
		if (changesMadeA)
			jtb.add(vocToEdit.getNameA(), tcsPanelA);
		if (changesMadeB)
			jtb.add(vocToEdit.getNameB(), tcsPanelB);
		JButton applyBtn = new JButton(LANG.getApply());
		applyBtn.addActionListener(new ApplyBtnListener());
		JButton backBtn = new JButton(LANG.getBack());
		backBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
				VMDM.setCurrentDialog(vsdFrame);
				vsdFrame.setVisible(true);
			}
		});
		JButton cancelBtn = new JButton(LANG.getCancel());
		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
				vsdFrame.dispose();
				new EditVocDialog(MAINVOC, SET, MAINFRAME, VMDM);
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(applyBtn);
		buttonPanel.add(backBtn);
		buttonPanel.add(cancelBtn);
		buttonPanel.setBackground(GuiTools.BGCOLOR);

		frame.getContentPane().add(BorderLayout.NORTH,
				new Header(LANG.getEffectsOnWordData()));
		frame.getContentPane().add(BorderLayout.CENTER, jtb);
		frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		if (GuiTools.getScreenSize().height < 768) {
			frame.showUp(10, 10);
		} else
			frame.showUpAtTopLeftCorner();
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				vsdFrame.dispose();
				frame.dispose();
				new EditVocDialog(MAINVOC, SET, MAINFRAME,VMDM);
			}
		});
		frame.setResizable(true);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frame.pack();
			}
		});
	}

	class TCsPanel extends JTabbedPane {

		public TCsPanel(Vector<String> editedTCs,
				HashMap<String, TermClass> oldTCs,
				HashMap<String, TermClass> nTCs, Vector<String> NSK,
				HashMap<String, ATCCDTermClassPanel> convertedTCs) {
			super();
			ATCCDTermClassPanel tcp;
			for (String oldTC : editedTCs) {
				tcp = new ATCCDTermClassPanel(frame, SET, oldTC, oldTCs
						.get(oldTC), oldTCs, nTCs, NSK);
				convertedTCs.put(oldTC, tcp);
				CustomScrollPane jsp = new CustomScrollPane(tcp);
				this.addTab(oldTC, jsp);
			}

		}
	}

	private class ApplyBtnListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			if(editedTCsA!=null && !editedTCsA.isEmpty()){
				for(String eTC : editedTCsA){
					for(Word w : vocToEdit.getWords()){
						if(w.getTermclassA().equals(eTC)){
							for(Meaning m : w.getMeaningVectorA()){
								//article
								if(m.hasSynonyms()){
									for(Term t : m.getSynonyms()){
										if(!convertedTCsA.get(eTC).getNewTermClass().hasPrefix())t.setArticle("");
										else t.setArticle(convertedTCsA.get(eTC).getConvertedArticle(t.getArticle()));
									}
								}
								if(!convertedTCsA.get(eTC).getNewTermClass().hasPrefix())m.setArticle("");
								else m.setArticle(convertedTCsA.get(eTC).getConvertedArticle(m.getArticle()));
								//meta information
								if(!vocToEdit.getTermclassA(eTC).isEmpty() && convertedTCsA.get(eTC).getNewTermClass().isEmpty()){
									for(String key : vocToEdit.getTermclassA(eTC).getKeys()){
										m.delMeta(key);
									}
								}
								else{
									if(convertedTCsA.get(eTC).getNewTermClass().getKeys()!=null){
										for(String key : convertedTCsA.get(eTC).getNewTermClass().getKeys()){
											m.putMetaElement(key, convertedTCsA.get(eTC).getConvertedMetaInformation(convertedTCsA.get(eTC).getOldMINameOf(key), m.getMetaElement(convertedTCsA.get(eTC).getOldMINameOf(key))));
										}
										for(String key : vocToEdit.getTermclassA(eTC).getKeys()){
											if(!convertedTCsA.get(eTC).getNewTermClass().getSortedKeys().contains(key)){
												m.delMeta(key);
											}
										}
									}
								}
							}
						}
					}
				}
			}
			if(editedTCsB!=null && !editedTCsB.isEmpty()){
				for(String eTC : editedTCsB){
					for(Word w : vocToEdit.getWords()){
						if(w.getTermclassB().equals(eTC)){
							for(Meaning m : w.getMeaningVectorB()){
								//article
								if(m.hasSynonyms()){
									for(Term t : m.getSynonyms()){
										if(!convertedTCsB.get(eTC).getNewTermClass().hasPrefix())t.setArticle("");
										else t.setArticle(convertedTCsB.get(eTC).getConvertedArticle(t.getArticle()));
									}
								}
								if(!convertedTCsB.get(eTC).getNewTermClass().hasPrefix())m.setArticle("");
								else m.setArticle(convertedTCsB.get(eTC).getConvertedArticle(m.getArticle()));
								//meta information
								if(!vocToEdit.getTermclassB(eTC).isEmpty() && convertedTCsB.get(eTC).getNewTermClass().isEmpty()){
									for(String key : vocToEdit.getTermclassB(eTC).getKeys()){
										m.delMeta(key);
									}
								}
								else{
									if(convertedTCsB.get(eTC).getNewTermClass().getKeys()!=null){
										for(String key : convertedTCsB.get(eTC).getNewTermClass().getKeys()){
											m.putMetaElement(key, convertedTCsB.get(eTC).getConvertedMetaInformation(convertedTCsB.get(eTC).getOldMINameOf(key), m.getMetaElement(convertedTCsB.get(eTC).getOldMINameOf(key))));
										}
										for(String key : vocToEdit.getTermclassB(eTC).getKeys()){
											if(!convertedTCsB.get(eTC).getNewTermClass().getSortedKeys().contains(key)){
												m.delMeta(key);
											}
										}
									}
								}
							}
						}
					}
				}
			}
			vocToEdit.setTermclasses(newTCsA, newTCsB);
			vocToEdit.setSortedTermClassKeysA(newSortedKeysA);
			vocToEdit.setSortedTermClassKeysB(newSortedKeysB);
			frame.dispose();
			vsdFrame.dispose();
			new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
		}
	}
}
